package com.crm.service.impl;

import com.crm.context.BaseContext;
import com.crm.dto.ClueDto;
import com.crm.dto.ClueQueryPagePramDto;
import com.crm.dto.GainDto;
import com.crm.dto.IsFalseDto;
import com.crm.mapper.ClueMapper;
import com.crm.pojo.BusinessRecord;
import com.crm.pojo.Clue;
import com.crm.result.PageResult;
import com.crm.service.ClueService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;

    /**
     * 分页查询线索信息
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult queryPageClue(ClueQueryPagePramDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        //查询线索表
        List<Clue> list = clueMapper.queryPageClue(dto);
        Page<Clue> page = (Page<Clue>) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增线索
     */
    @Override
    public void addClue(ClueDto dto) {
        Clue clue = new Clue();
        BeanUtils.copyProperties(dto, clue);
        //补齐数据
        clue.setStatus("1");
        clue.setOwner(String.valueOf(BaseContext.getCurrentId()));
        clue.setOwnerTime(LocalDateTime.now());
        clue.setLevel("0");
        clue.setSubject(2);
        clue.setActivityId(1001L);
        clueMapper.addClue(clue);
    }

    /**
     * 根据线索id查询
     *
     * @param id
     * @return
     */
    @Override
    public Clue queryById(Long id) {
        return clueMapper.queryById(id);
    }

    @Override
    public PageResult queryPagePool(ClueQueryPagePramDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<Clue> list = clueMapper.queryPagePool(dto);
        Page<Clue> page = (Page<Clue>) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public PageResult queryRecord(Long clueId) {
        List<BusinessRecord> list = clueMapper.queryRecord(clueId);
        return new PageResult(10L, list);
    }

    @Override
    public void gainClue(GainDto gain) {
        log.info("捞取");
        clueMapper.gainClue(gain);
    }

    @Override
    public void isFalse(Long id, IsFalseDto dto) {
        clueMapper.isFalse(id, dto);
    }


}
